/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * NOTE:
 * Derivative work based on the Google IO 2011 Schedule app released for open source
 * http://code.google.com/p/iosched/
 */

package edu.bsu.android.gtv.apps.bconnected.ui; 

import edu.bsu.android.gtv.apps.bconnected.R;
import edu.bsu.android.gtv.apps.bconnected.objects.Person;
import edu.bsu.android.gtv.apps.bconnected.ui.base.BaseActivity;
import edu.bsu.android.gtv.apps.bconnected.util.AnalyticsUtils;
import edu.bsu.android.gtv.apps.bconnected.util.Constants;
import edu.bsu.android.gtv.apps.bconnected.util.DisclaimerHelper;
import edu.bsu.android.gtv.apps.bconnected.util.LoginHelper;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

/**
 * Home activity for the application
 * @author Kyle Parker, University Computing Services
 */
// TODO: Review all intents and determine whether or not they still work - maps, contacts and share
// TODO: Determine what intents are available - map, contacts, share (supposedly "share" and "map"
// TODO: Change onFocusUp for the Calendar - after viewing a list, the up command will jump to the schedule instead of About Me
// TODO: Selected+focus on the tab icons
// TODO: Add a video gallery to the home screen instead of the news ticker
// TODO: Review data types for the objects - should really use the correct type and not default to "string"
public class HomeActivity extends BaseActivity {
	
	private Activity mActivity;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivity = this;
        
        // If the EULA preference does not exist, do not display the message.  The student must log in first in order to create 
        // the preference and then accept or decline.  If "has accepted" is checked immediately, it will appear twice the first 
        // time the app is installed - once when opening the app and once again after the student logs in.  This will eliminate 
        // the initial display on app open.
        if (DisclaimerHelper.disclaimerExists(mActivity)) {
        	if (!DisclaimerHelper.hasAcceptedDisclaimer(mActivity)) {
        		DisclaimerHelper.showDisclaimer(false, mActivity);
        	}
        }
        
        // Google analytics
        AnalyticsUtils.getInstance(mActivity).trackPageView("/Home");

        mActivity.setContentView(R.layout.activity_home); 
        handleLogin();
        
        getExtras();
        getActivityHelper().setupActionBar(true, "Ball State University");
    }

    /**
     * Get the extras from the Intent
     */
    public void getExtras() {
		Bundle extras = mActivity.getIntent().getExtras(); 
		
		if (extras != null) {
			if (extras.getString(Constants.EXTRA_LOGIN_STATUS) != null) {
				String status = null;

				if (extras.getString(Constants.EXTRA_LOGIN_STATUS).equals(Integer.toString(Constants.LOGIN_STATUS_FAILED))) {
					status = mActivity.getString(R.string.toast_login_error); 
				} else if (extras.getString(Constants.EXTRA_LOGIN_STATUS).equals(Integer.toString(Constants.LOGIN_STATUS_LOGOUT))) {
					status = mActivity.getString(R.string.toast_login_logout); 					
				} else if (extras.getString(Constants.EXTRA_LOGIN_STATUS).equals(Integer.toString(Constants.LOGIN_STATUS_CANCELLED))) {
					status = mActivity.getString(R.string.toast_login_cancelled); 					
				} else if (extras.getString(Constants.EXTRA_LOGIN_STATUS).equals(Integer.toString(Constants.LOGIN_STATUS_LOGIN))) {
					status = mActivity.getString(R.string.toast_login_successful);	 					
				} else if (extras.getString(Constants.EXTRA_LOGIN_STATUS).equals(Integer.toString(Constants.LOGIN_STATUS_NOT_LOGGED_IN))) {
					status = mActivity.getString(R.string.toast_login_not_logged_in);					
				}
				
                Toast.makeText(mActivity, status, Toast.LENGTH_LONG).show();	
			}
		}
    }
    
    /**
     * Determine if the person has logged in and whether there is a person object in the database
     * If not, set the hasLoggedIn preference to false
     */
    private void handleLogin() {
    	Person person = LoginHelper.checkLogin(mActivity);
    	
    	if (person == null) {
            LoginHelper.setLogin(mActivity, false);
    	}    	
    }
}
